class LimitBuyDecorate extends eui.Component{
	constructor() {
		super();
		this.skinName="skins.scene.LimitBuyDecorateSkin";
		this.items = [];
	}
	public onInfoBack(data){
		this.currentInfo = data;
		this.updateDisplay();
	}
	public onBuyBack(data){
		this.currentInfo = data;

	}
	private items: FuckLimitDec[];
	public currentInfo;
	public config;
	private currentGroup;
	private updateDisplay(){
		var date = Model.referDate;
		date.setTime(Model.SYSTIME);
		
		this.currentGroup = this.config.groups[this.currentInfo.groupId];
		if( this.currentGroup.vipLevel != 0){
			this.lab_0.text = "VIP"+this.currentGroup.vipLevel+"用户";
		} else {
			this.lab_0.text = "所有用户";
		}

		this.lab_1.text = (date.getMonth()+1)+"月"+date.getDate()+"日 "+this.config.startTime+"~"+this.config.endTime;
		this.lab_3.text = this.currentGroup.diamond + "";
		var totalCost = 0;
		var goodInfo: GoodInfo;
		var decs = this.currentGroup.ornamentIds;
		var len = decs.length;
		var item: FuckLimitDec;
		for (var i = 0; i < len; i++){
			if( i < this.items.length){
				item = this.items[i];
			}else{
				item = new FuckLimitDec();
				this.items.push(item);
				item.x = 92 * i;
				this.con.addChild(item);
			}
			goodInfo = Model.GetGoodInfoById(decs[i]);
			totalCost += goodInfo.price;
			item.setData(decs[i]);
		}
		this.lab_2.text = totalCost + "";
	}
	private onOk(evt: egret.TouchEvent) {
		SoundMgr.getInstance().playEffect(SoundMgr.BTN);
		if (this.currentInfo.purchased){
			ShowHint("每人每天只能购买一次");
			return;
		}
		if(this.currentGroup.vipLevel > Model.me.vipLevel){
			ShowHint("今日活动只对vip" + this.currentGroup.vipLevel + "用户开放");
			return;
		}

		if (Model.me.diamond >= this.currentGroup.diamond){
			window["__limitDecBuy"]();
		}else{
			this.hide();
			BuyDiamondPanel.getInstance().show();
		}
	}
	private onClose(evt: egret.TouchEvent) {
		SoundMgr.getInstance().playEffect(SoundMgr.BTN);
		this.hide();
	}
	public hide(){
		DisplayController.instance().removeFromAlert(GameConst.GrayMask);
		DisplayController.instance().removeFromAlert(this);
		FrameMgr.getInstance().delController(this.name);
	}

	public show(){
	 	if( !DisplayController.instance().stage) return;
	 	if( this.currentInfo == null){
			  window["__limitDecInfo"]();
			  this.config = ActivityMgr.getInstance().getActivityConfigById(ActivityMgr.ACTIVITY_LIMIT_BUY_DEC).config[0];
		  }
		  DisplayController.instance().addToAlert(GameConst.GrayMask);
		  DisplayController.instance().addToAlert(this);
	}

	public img_pop_bottom_1:eui.Image;
	public img_close:eui.Image;
	public img_ok:eui.Image;
	public lab_0:eui.Label;
	public lab_1:eui.Label;
	public lab_2:eui.Label;
	public lab_3:eui.Label;
	public con:eui.Group;
	public partAdded(name: string, instance:any){
		super.partAdded(name, instance);
		if(name == "img_pop_bottom_1"){
			this.img_pop_bottom_1=instance;
		}else if(name == "img_close"){
			this.img_close=instance;
			this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
		} else if (name == "img_ok") {
			this.img_ok = instance;
			this.img_ok.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onOk, this);
		} else if (name == "lab_1"){
			this.lab_1=instance;
		} else if (name == "lab_1"){
			this.lab_1=instance;
		} else if (name == "lab_2"){
			this.lab_2=instance;
		} else if (name == "lab_3"){
			this.lab_3=instance;
		} else if (name == "con"){
			this.con=instance;
		}
	}

	public measure(){
		super.measure();
		this.x = GameConst.StageW - this.measuredWidth >> 1;
		this.y = GameConst.StageH - this.measuredHeight >> 1;
	}
		private static _instance :LimitBuyDecorate;
	public static getInstance():LimitBuyDecorate{
		if(LimitBuyDecorate._instance == null){
			LimitBuyDecorate._instance = new LimitBuyDecorate();
		}
		return LimitBuyDecorate._instance;
	}
}